package com.eco.fanliapp.utils;


import org.joda.time.DateTime;
import org.springframework.util.Assert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by xiaoxx
 */
public class DateFormatUtils {

    //joda-time
    //str->Date
    //Date->str

    public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";


    public static final String STANDARD_FORMAT5 = "HH:mm:ss";

    public static final String STANDARD_FORMAT1 = "yyyy-MM-dd";

    public static final String STANDARD_FORMAT2 = "yyyyMMdd";
    public static final String STANDARD_FORMAT3 = "yyyyMMddHH";

    public static final String STANDARD_FORMAT4 = "yyyy年MM月dd日";

    public static final String STANDARD_FORMAT6 = "MM月dd日  HH:mm";
    public static final String STANDARD_FORMAT8 = "MM月dd日";

    public static final String STANDARD_FORMAT7 = "yyyy-MM-dd HH:mm";


    public static final String STANDARD_FORMAT9 = "yyyy-MM-dd-HH";

    public static final String STANDARD_FORMAT10 = "yyyyMMddhhmmss";

    /**
     * @param minute
     * @return "yyyy-MM-dd HH:mm:ss"
     */
    public static String getTimeByMinute(int minute) {

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, minute);

        return new SimpleDateFormat(STANDARD_FORMAT).format(calendar.getTime());

    }

    public static Date string2date(String source) {
        SimpleDateFormat format1 = new SimpleDateFormat("yyy-MM-dd");
        Date date = null;
        try {
            date = format1.parse(source);
        } catch (Exception e) {

        }
        return date;
    }

    public static Date string2dateTime(String source) {
        SimpleDateFormat format1 = new SimpleDateFormat(STANDARD_FORMAT);
        Date date = null;
        try {
            date = format1.parse(source);
        } catch (Exception e) {

        }
        return date;
    }

    /**
     * 获取未来 第 past 天的日期
     *
     * @param past
     * @return
     */
    public static Date getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        return today;
    }

    /**
     * 功能:两个日期相隔天数
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return 返回相减后的日期
     */
    public static int diffDate(Date startDate, Date endDate) {
        long endMillis = endDate.getTime();
        long startMillis = startDate.getTime();
        long s = (endMillis - startMillis) / (24 * 3600 * 1000);
        return (int) s;
    }

    public static int diffDateLong(Date startDate, Date endDate) {
        long endMillis = endDate.getTime();
        long startMillis = startDate.getTime();
        long s = (endMillis - startMillis);
        return (int) s;
    }

    public static boolean diffTimeDate(Date startDate, Date endDate) {
        long endMillis = endDate.getTime();
        long startMillis = startDate.getTime();
        long s = startMillis - endMillis;
        return s > 0 ? true : false;
    }

    /**
     * 功能：指定日期加上指定天数
     *
     * @param date 日期
     * @param day  天数
     * @return 返回相加后的日期
     */
    public static Date addDate(Date date, int day) {


        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600L * 1000L);
        return c.getTime();
    }

    /**
     * 功能：指定日期加上指定小时
     *
     * @param date 日期
     * @param time 小时
     * @return 返回相加后的日期
     */
    public static Date addTime(Date date, int time) {


        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) + time * 3600L * 1000L);
        return c.getTime();
    }

    public static Date beforeDate(Date date, int day) {
        Calendar c = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        c.setTimeInMillis(getMillis(zero) - ((long) day) * 24 * 3600 * 1000);
        return c.getTime();
    }

    /**
     * 返回当前月开始一天
     *
     * @return
     */
    public static Date monthBeginDate() {
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        cal_1.set(Calendar.HOUR_OF_DAY, 0);
        cal_1.set(Calendar.MINUTE, 0);
        cal_1.set(Calendar.SECOND, 0);
        Date zero = cal_1.getTime();
        return zero;
    }

    /**
     * 返回上一个月开始一天
     *
     * @return
     */
    public static Date lastMonthBeginDate() {
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        cal_1.set(Calendar.HOUR_OF_DAY, 0);
        cal_1.set(Calendar.MINUTE, 0);
        cal_1.set(Calendar.SECOND, 0);
        Date zero = cal_1.getTime();
        return zero;
    }

    /**
     * 返回上一个月的最后一天
     *
     * @return
     */
    public static Date lastMonthEndDate() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, 0);
        ca.set(Calendar.HOUR_OF_DAY, 23);
        ca.set(Calendar.MINUTE, 59);
        ca.set(Calendar.SECOND, 59);
        Date zero = ca.getTime();
        return zero;
    }

    /**
     * 返回今天0时
     *
     * @return Date dayStart = DateTime.now().withMillisOfDay(0).plusDays(-1).toDate();
     */
    @SuppressWarnings("deprecation")
    public static Date todayBeginDate() {
        Calendar ca = Calendar.getInstance();
        Date d = new Date();
        ca.set(Calendar.DAY_OF_MONTH, d.getDate());
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.set(Calendar.MINUTE, 0);
        ca.set(Calendar.SECOND, 0);
        Date zero = ca.getTime();
        return zero;
    }

    /**
     * 返回昨天0时
     *
     * @return
     */
    @SuppressWarnings("deprecation")
    public static Date yestodayBeginDate() {
        Calendar ca = Calendar.getInstance();
        Date d = new Date(new Date().getTime() - 86400000);
        ca.set(Calendar.DAY_OF_MONTH, d.getDate());
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.set(Calendar.MINUTE, 0);
        ca.set(Calendar.SECOND, 0);
        Date zero = ca.getTime();
        return zero;
    }

    public static String subDay(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_FORMAT9);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.DAY_OF_MONTH, -1);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }


    public static Date monthEndDate() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));

        ca.set(Calendar.HOUR_OF_DAY, 23);
        ca.set(Calendar.MINUTE, 59);
        ca.set(Calendar.SECOND, 59);
        Date zero = ca.getTime();
        return zero;
    }

    /**
     * 返回当前年月日
     *
     * @return
     */
    public static String getNowDate() {
        Date date = new Date();
        String nowDate = new SimpleDateFormat(STANDARD_FORMAT4).format(date);
        return nowDate;
    }

    /**
     * 返回当前年月日时分
     *
     * @return
     */
    public static String getNowDateMin(Date time) {
        String nowDate = new SimpleDateFormat(STANDARD_FORMAT7).format(time);
        return nowDate;
    }

    /**
     * 返回当前年月日
     *
     * @return
     */
    public static String getNowDateDay(Date date) {
        String nowDate = new SimpleDateFormat(STANDARD_FORMAT1).format(date);
        return nowDate;
    }

    /**
     * 返回当前年月日
     *
     * @return
     */
    public static String getYearsterDateDay() {

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date date = cal.getTime();

        String nowDate = new SimpleDateFormat(STANDARD_FORMAT1).format(date);
        return nowDate;
    }

    /**
     * 区分上午下午，返回当前日期
     * 返回当前年月日
     *
     * @return
     */
    public static String getNowDateDay2() {
        LocalDateTime now = LocalDateTime.now(ZoneId.of("+8"));
        String pattern = "yyyyMMddHHmmss";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        String format = formatter.format(now);
        return format;
    }

    public static String dateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_FORMAT);
        String str = sdf.format(date);
        return str;
    }

    /**
     * 返回当前年月日
     *
     * @return
     */
    public static String getNowDateHour(Date date) {
        String nowDate = new SimpleDateFormat(STANDARD_FORMAT9).format(date);
        return nowDate;
    }

    /**
     * 返回当前年份
     *
     * @return
     */
    public static int getYear() {
        Date date = new Date();
        String year = new SimpleDateFormat("yyyy").format(date);
        return Integer.parseInt(year);
    }

    /**
     * 返回当前月份
     *
     * @return
     */
    public static int getMonth() {
        Date date = new Date();
        String month = new SimpleDateFormat("dd").format(date);
        return Integer.parseInt(month);
    }

    public static int getDate() {
        Date date = new Date();
        String month = new SimpleDateFormat("dd").format(date);
        return Integer.parseInt(month);
    }

    /**
     * 功能：返回毫秒
     *
     * @param date
     * @return
     */
    public static long getMillis(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }

    /**
     * 获取今天开始的时间戳
     *
     * @return
     */
    public static long getStartTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime().getTime();
    }

    /**
     * 获取今天结束的时间戳
     *
     * @return
     */
    public static long getEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.setTime(new Date());
        todayEnd.set(Calendar.HOUR, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime().getTime();
    }

    /**
     * 获取昨天结束的时间戳
     *
     * @return
     */
    public static long getYesterdayEndTime() {
        Calendar yesterdayEnd = Calendar.getInstance();
        //yesterdayEnd.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        yesterdayEnd.setTime(new Date());
        yesterdayEnd.add(Calendar.DATE, -1);
        yesterdayEnd.set(Calendar.HOUR_OF_DAY, 23);
        yesterdayEnd.set(Calendar.MINUTE, 59);
        yesterdayEnd.set(Calendar.SECOND, 59);
        yesterdayEnd.set(Calendar.MILLISECOND, 999);
        return yesterdayEnd.getTime().getTime();

    }

    /**
     * 获取昨天开始的时间戳
     *
     * @return
     */
    public static long getYesterdayStartTime() {
        Calendar yesterdayStart = Calendar.getInstance();
        yesterdayStart.setTime(new Date());
        yesterdayStart.add(Calendar.DATE, -1);
        yesterdayStart.set(Calendar.HOUR_OF_DAY, 0);
        yesterdayStart.set(Calendar.MINUTE, 0);
        yesterdayStart.set(Calendar.SECOND, 0);
        yesterdayStart.set(Calendar.MILLISECOND, 0);
        return yesterdayStart.getTime().getTime();

    }

    /**
     * 将时间戳转换为时间
     *
     * @param s
     * @return yyyyMMddHH
     */
    public static String stampToDate(Long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STANDARD_FORMAT3);
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static String stampToDate1(Long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STANDARD_FORMAT2);
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将时间戳转换为时间 指定格式
     *
     * @param s
     * @param formatType
     * @return
     */
    public static String stampToDate2(Long s, String formatType) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatType);
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static Date stampToDate3(Long s, String formatType) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatType);
        Date date = new Date(s);
        return date;
    }

    /**
     * 将时间转换为时间戳
     *
     * @param s
     * @return yyyy-MM-dd
     */
    public static String dateToStamp(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STANDARD_FORMAT1);
        Date date;
        try {
            date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            res = String.valueOf(ts);
            return res;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将时间转换为时间戳
     *
     * @param s
     * @return yyyy-MM-dd
     */
    public static String dateToStamp1(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STANDARD_FORMAT3);
        Date date;
        try {
            date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            res = String.valueOf(ts);
            return res;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String transferLongToDate(Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_FORMAT);
        Date date = new Date(millSec);
        return sdf.format(date);
    }


    public static Long timestamp() {
        long timestamp = System.currentTimeMillis();
        return timestamp;
    }

    // date类型转换为String类型
    // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    // data Date类型的时间
    public static Long stringTimeToLong(String strTime, String formatType) {

        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        try {
            date = formatter.parse(strTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String format = new SimpleDateFormat(formatType).format(date);

        return Long.parseLong(format);
    }

    public static Long dateToString(Date date, String formatType) {
        String format = new SimpleDateFormat(formatType).format(date);
        return Long.parseLong(format);
    }

    // long类型转换为String类型
    // currentTime要转换的long类型的时间
    // formatType要转换的string类型的时间格式
    /*public static String longToString(long currentTime, String formatType)
            throws ParseException {
        Date date = longToDate(currentTime, formatType); // long类型转成Date类型
        String strTime = dateToString(date, formatType); // date类型转成String
        return strTime;
    }*/

    // string类型转换为date类型
    // strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    // HH时mm分ss秒，
    // strTime的时间格式必须要与formatType的时间格式相同
    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }

    // long转换为Date类型
    // currentTime要转换的long类型的时间
    // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
   /* public static Date longToDate(long currentTime, String formatType)
            throws ParseException {
        Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
        String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
        Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
        return date;
    }*/

    // string类型转换为long类型
    // strTime要转换的String类型的时间
    // formatType时间格式
    // strTime的时间格式和formatType的时间格式必须相同
    public static long stringToLong(String strTime, String formatType) throws ParseException {
        Date date = stringToDate(strTime, formatType); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            long currentTime = dateToLong(date); // date类型转成long类型
            return currentTime;
        }
    }

    // date类型转换为long类型
    // date要转换的date类型的时间
    public static long dateToLong(Date date) {
        return date.getTime();
    }

    /**
     * 将字符串转日期成Long类型的时间戳，格式为：yyyy-MM-dd HH:mm:ss
     */
    public static Long convertTimeToLong(String time) {
        Assert.notNull(time, "time is null");
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern(STANDARD_FORMAT);
        LocalDateTime parse = LocalDateTime.parse(time, ftf);
        return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static String dateUtils(Long sjc) {
        Calendar instance = Calendar.getInstance();
        Date date = new Date(sjc);
        instance.setTime(date);
        String s = stampToDate2(sjc, STANDARD_FORMAT6);
        String timeAfter = s.substring(0, s.indexOf(" "));//月日
        String timeBefor = s.substring(s.indexOf(" "));//时分
        String week = getDayOfWeekByDate(instance);//周
        String shang = (instance.get(Calendar.AM_PM) == Calendar.getInstance().AM) ? "上午" : "下午";//上下午
        int format = format(date);//1 今天 2 昨天 3 昨天之后
        if (format == 1) return shang + timeBefor;
        else if (format == 2) return week + shang + timeBefor;
        else if (format == 3) return timeAfter + week + shang + timeBefor;
        return null;
    }

    public static String getDayOfWeekByDate(Calendar instance) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        int w = instance.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) w = 0;
        return weekDays[w];
        /*SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        String week = sdf.format(date);
        return week;*/
    }

    private static int format(Date date) {
        DateTime now = new DateTime();
        DateTime today_start = new DateTime(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth(), 0, 0, 0);
        DateTime today_end = today_start.plusDays(1);
        DateTime yesterday_start = today_start.minusDays(1);
        if (date.after(today_start.toDate()) && date.before(today_end.toDate())) {
            return 1;
        } else if (date.after(yesterday_start.toDate()) && date.before(today_start.toDate())) {
            return 2;
        } else {
            return 3;
        }
    }

    public static String getCurrentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_FORMAT);
        return sdf.format(new Date());
    }

    public static void main(String[] args) {
        String yearsterDateDay = getYearsterDateDay();
        System.out.println(yearsterDateDay);
    }
}
